<?php
declare (strict_types=1);

namespace app\api\model\v1;

use think\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class Unit extends AppModel
{
    //


    const ATTRIBUTE_ZERO = 0;
    const ATTRIBUTE_ONE = 1;
    const ATTRIBUTE_TWO = 2;
    const ATTRIBUTE_THREE = 3;
    const ATTRIBUTE_FOUR = 4;
    
    /**
     * @var string[]
     * @User: 刘海龙
     * @Date: 2021/10/26
     * @Time: 14:48
     * 网点属性
     */
    public static $is_attribute = array(
        self::ATTRIBUTE_ZERO => '默认',
        self::ATTRIBUTE_ONE => '邮政',
        self::ATTRIBUTE_TWO => '综合',
        self::ATTRIBUTE_THREE => '专项',
        self::ATTRIBUTE_FOUR => '普服'
    );


    const LEVEL_ZERO = 0;
    const LEVEL_ONE = 1;
    const LEVEL_TWO = 2;
    const LEVEL_THREE = 3;

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/5/7 11:49
     * @var string[]
     * 单位层级
     */
    public static $is_level = array(
        self::LEVEL_ZERO => '全省数据',
        self::LEVEL_ONE => '市公司数据',
        self::LEVEL_TWO => '县区数据',
        self::LEVEL_THREE => '个人统计'
    );

    public function industry()
    {
        return $this->belongsTo(Industry::class, 'industry_id');
    }

    public function users()
    {
        return $this->hasMany(Users::class, 'unit_id');
    }

    //总计统计
    public function examAudit()
    {
        return $this->hasMany(AuditExam::class, 'unit_id')->field('id,unit_id,date_of');
    }
    public function children()
    {
        return $this->hasMany(AuditExam::class, 'unit_id')->field('id,unit_id,unit_id as unit_title,date_of,status as exam_audit_count,original,deduct_marks,final_score');
    }

    public function unitTotal(){
        return $this->hasOne(UnitTotal::class,'unit_id')->field('id,unit_id,total,persons_total,answer_persons_total');
    }


    public function EveryDayAnswerCensus(){
        return $this->hasMany(EveryDayAnswerCensus::class,'unit_id');
    }


    public function examAudits()
    {
        return $this->hasMany(AuditExam::class, 'unit_id')->field('id,unit_id,date_of')->when();
    }

    //满分数
    public function fullMarks()
    {
        return $this->hasMany(AuditExam::class, 'unit_id')->field('id,unit_id,date_of,deduct_marks');
    }

    //扣分数
    public function points()
    {
        return $this->hasMany(AuditExam::class, 'unit_id')->field('id,unit_id,date_of,deduct_marks');
    }

    //待审数
    public function pending()
    {
        return $this->hasMany(AuditExam::class, 'unit_id')->field('id,unit_id,date_of,status');
    }


    public function topicUserDay()
    {
        return $this->hasMany(TopicUserDay::class, 'unit_id');
    }


    public function monthlyTest()
    {
        return $this->hasMany(MonthlyTest::class, 'unit_id');
    }


    






    public static function saveUnitExcel($arrData, $create_id)
    {
        /**
         *
         *   `id` int(11) NOT NULL AUTO_INCREMENT,
         * `unit_title` varchar(20) NOT NULL COMMENT '单位名称',
         * `pid` int(30) NOT NULL DEFAULT '0' COMMENT '父ID',
         * `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态',
         * `create_id` int(30) NOT NULL DEFAULT '1' COMMENT '创建人id',
         * `area_id` int(30) NOT NULL DEFAULT '0' COMMENT '所属地区id',
         * `industry_id` int(30) NOT NULL DEFAULT '0' COMMENT '所属行业',
         * `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
         * `update_time` timestamp NULL DEFAULT NULL,
         * `delete_time` timestamp NULL DEFAULT NULL,
         * `level` tinyint(1) NOT NULL DEFAULT '0' COMMENT '层级 0 1 2 省市区县',
         * `attribute` tinyint(1) NOT NULL DEFAULT '0' COMMENT '属性 0 1 2 默认/邮政/综合',
         */
        if (!empty($arrData)) {
            foreach ($arrData as $key => $v) {
                $city = Unit::where(['unit_title' => trim($v['city'])])->find();
                if (is_null($city)) {
                    $unit_id = Unit::create([
                        'unit_title' => $v['city'],
                        'pid' => 230,
                        'create_id' => $create_id,
                        'industry_id' => 1,
                        'level' => 0
                    ])->id;
                } else {
                    $unit_id = $city->id;
                }
                if (isset($v['item'])) {
                    foreach ($v['item'] as $child_key => $child_v) {
                        $county = Unit::where(['unit_title' => trim($child_v['county'])])->find();
                        if (is_null($county)) {
                            $unit_county = Unit::create([
                                'unit_title' => $child_v['county'],
                                'pid' => $unit_id,
                                'create_id' => $create_id,
                                'industry_id' => 1,
                                'level' => 1
                            ])->id;
                        } else {
                            $unit_county = $county->id;
                        }
                        if (isset($child_v['item'])) {
                            foreach ($child_v['item'] as $child_child_key => $child_child_v) {
                                $title = Unit::where(['unit_title' => trim($child_child_v['title'])])->find();
                                if (is_null($title)) {
                                    Unit::create([
                                        'unit_title' => $child_child_v['title'],
                                        'pid' => $unit_county,
                                        'create_id' => $create_id,
                                        'industry_id' => 1,
                                        'level' => 2
                                    ]);
                                }
                            }
                        }
                    }
                }
            }
            $result = true;
        } else {
            $result = false;
        }
        return $result;
    }
//    public static function saveUnitExcel($arrData, $create_id)
//    {
//        if (!empty($arrData)) {
//            foreach ($arrData as $key => $v) {
//                self::create([
//                    'unit_title' => $v[0] ?? '',
//                    'pid' => 15,
//                    'create_id'=>$create_id,
//                    'industry_id' => 1,
//                ]);
//            }
//            $result = true;
//        } else {
//            $result = false;
//        }
//        return $result;
//    }
}
